package com.zucai365.football.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.wcj.common.service.impl.WcjServiceImpl;
import com.zucai365.football.bean.FootballPlayerBean;
import com.zucai365.football.bean.GameFireBean;
import com.zucai365.football.bean.ShouFaBean;
import com.zucai365.football.bean.TeamDzErrorLogBean;
import com.zucai365.football.bean.TeamPlayerBean;
import com.zucai365.football.dao.ShouFaDao;
import com.zucai365.football.service.FootballPlayerService;
import com.zucai365.football.service.GameFireService;
import com.zucai365.football.service.LianSaiService;
import com.zucai365.football.service.LianSaiYearService;
import com.zucai365.football.service.ShouFaService;
import com.zucai365.football.service.TeamDzErrorLogService;
import com.zucai365.football.service.TeamPlayerService;
import com.zucai365.football.utils.ZuCaiUtils;

@Service("shouFaService")
public class ShouFaServiceImpl extends WcjServiceImpl<ShouFaBean> implements ShouFaService{
	
	@Autowired
	@Qualifier("shouFaDao")
	private ShouFaDao shouFaDao;

	public ShouFaDao getShouFaDao() {
		return shouFaDao;
	}

	public void setShouFaDao( ShouFaDao shouFaDao ) {
		this.shouFaDao = shouFaDao;
	}
	
	@Autowired
	@Qualifier("footballPlayerService")
	private FootballPlayerService footballPlayerService;
	
	public FootballPlayerService getFootballPlayerService() {
		return footballPlayerService;
	}

	public void setFootballPlayerService(FootballPlayerService footballPlayerService) {
		this.footballPlayerService = footballPlayerService;
	}
	
	@Autowired
	@Qualifier("lianSaiService")
	private LianSaiService lianSaiService;
	
	@Autowired
	@Qualifier("lianSaiYearService")
	private LianSaiYearService lianSaiYearService;
	
	public LianSaiService getLianSaiService() {
		return lianSaiService;
	}

	public void setLianSaiService(LianSaiService lianSaiService) {
		this.lianSaiService = lianSaiService;
	}

	public LianSaiYearService getLianSaiYearService() {
		return lianSaiYearService;
	}

	public void setLianSaiYearService(LianSaiYearService lianSaiYearService) {
		this.lianSaiYearService = lianSaiYearService;
	}
	
	@Autowired
	@Qualifier("teamPlayerService")
	private TeamPlayerService teamPlayerService;
	
	public TeamPlayerService getTeamPlayerService() {
		return teamPlayerService;
	}

	public void setTeamPlayerService(TeamPlayerService teamPlayerService) {
		this.teamPlayerService = teamPlayerService;
	}
	
	@Autowired
	@Qualifier("gameFireService")
	private GameFireService gameFireService;
	
	public GameFireService getGameFireService() {
		return gameFireService;
	}

	public void setGameFireService(GameFireService gameFireService) {
		this.gameFireService = gameFireService;
	}
	
	@Autowired
	@Qualifier("teamDzErrorLogService")
	private TeamDzErrorLogService teamDzErrorLogService;
	
	public TeamDzErrorLogService getTeamDzErrorLogService() {
		return teamDzErrorLogService;
	}

	public void setTeamDzErrorLogService(TeamDzErrorLogService teamDzErrorLogService) {
		this.teamDzErrorLogService = teamDzErrorLogService;
	}

	@Override
	public void saveGameFireInfo( List<ShouFaBean> shuofaList , List<GameFireBean> gameFireList ) {
		
		//检查出场球员
		checkPlayer( shuofaList );
		//检查球员比赛状况
		checkGameFireInfo( shuofaList , gameFireList );
		
		List<ShouFaBean> sfList = new ArrayList<ShouFaBean>();
		for( ShouFaBean shouFa : shuofaList ){
			
			ShouFaBean sf = getShouFa( shouFa.getLiansaiYearId(), shouFa.getLiansaiId(), shouFa.getTeamDz().getId(), shouFa.getLunci(), shouFa.getPlayerId() );
			if( sf == null ){
				sfList.add( shouFa );
			}else{
				sf.setPlayingSumTime( shouFa.getPlayingSumTime() );
				sfList.add( sf );
			}
			
		}
		
		List<TeamDzErrorLogBean> teamDzErrorLogList = new ArrayList<TeamDzErrorLogBean>();
		for( int i=0;i<gameFireList.size(); i++){
			GameFireBean gameFire = gameFireList.get(i);
			if( gameFire.getFootballPlayer() == null ){
				
				TeamDzErrorLogBean teamDzErrorLog = new TeamDzErrorLogBean();
				teamDzErrorLog.setLiansaiId( gameFire.getTeamDz().getLiansai().getId() );
				teamDzErrorLog.setLiansaiYearId( gameFire.getTeamDz().getLiansaiYear().getId() );
				teamDzErrorLog.setTeamDz( gameFire.getTeamDz() );
				teamDzErrorLog.setLunci( gameFire.getTeamDz().getLunci() );
				teamDzErrorLog.setStatus( 0 );
				teamDzErrorLog.setErrorMsg( gameFire.getTeamDz().getZdName() + "vs" +  gameFire.getTeamDz().getKd().getFootballTeamName() + "==="
						+gameFire.getFootballTeam().getFootballTeamName() + "===" 
						+ gameFire.getPersonName() + "==" + gameFire.getJerseyNumber() +"号===" + gameFire.getEvent() 
						+ "===" + gameFire.getEventTime() + "分钟" );
				teamDzErrorLog.setEvent( gameFire.getEvent() );
				teamDzErrorLog.setEventTime( gameFire.getEventTime() );
				teamDzErrorLog.setJerseyNumber( gameFire.getJerseyNumber() );
				
				teamDzErrorLogList.add( teamDzErrorLog );
				
				gameFireList.remove( gameFire );
				i=0;
			}
		}
		
		this.shouFaDao.saveAll( sfList );
		this.gameFireService.saveGameFire( gameFireList );
		
		this.teamDzErrorLogService.saveTeamDzErrorLog( teamDzErrorLogList );
	}
	
	/**
	 * 检查上场的球员在球员库中和球队中是否存在，不存在就插入。
	 * @param shuofaList
	 */
	private void checkPlayer( List<ShouFaBean> shuofaList ){
		
		List<FootballPlayerBean> footballPlayerList= new ArrayList<FootballPlayerBean>();
		List<TeamPlayerBean> teamPlayerList= new ArrayList<TeamPlayerBean>();
		
		for( ShouFaBean shouFa : shuofaList){
			FootballPlayerBean footballPlayer = this.footballPlayerService.getFootballPlayer( shouFa.getPlayerId() );
			if( footballPlayer == null ){ //没有球员资料
				System.out.println( shouFa.getPersonName() + "不存在，添加" );
				
				footballPlayer = new FootballPlayerBean();
				footballPlayer.setPlayerName( shouFa.getPersonName() );
				footballPlayer.setPlayerId( shouFa.getPlayerId() );
				
				footballPlayerList.add( footballPlayer );
				
				TeamPlayerBean teamPlayer = new TeamPlayerBean();
				teamPlayer.setFootballPlayer(footballPlayer);
				teamPlayer.setFootballTeam( shouFa.getFootballTeam() );
				teamPlayer.setFootballTeamName( shouFa.getFootballTeam().getFootballTeamName() );
				teamPlayer.setLiansai( ZuCaiUtils.getLianSaiById( shouFa.getLiansaiId() ) );
				teamPlayer.setLiansaiYear( ZuCaiUtils.getLianSaiYearById( shouFa.getLiansaiYearId() ) );
				teamPlayer.setPlayerName( shouFa.getPersonName() );
				
				teamPlayerList.add( teamPlayer );
			}else{			
				//查询该球队是否有该球员，如果没有需要插入一条
				TeamPlayerBean teamPlayer = this.teamPlayerService.getTeamPlayer( shouFa.getLiansaiYearId(), shouFa.getLiansaiId(),
						shouFa.getFootballTeam().getId(), shouFa.getPlayerId() );
				
				if( teamPlayer == null ){
					System.out.println( shouFa.getPersonName() + "不存在" + shouFa.getFootballTeam().getFootballTeamName() + "中  " + shouFa.getPlayerId()  );
					teamPlayer = new TeamPlayerBean();
					teamPlayer.setFootballPlayer(footballPlayer);
					teamPlayer.setPlayerName( shouFa.getPersonName()  );
					teamPlayer.setFootballTeam( shouFa.getFootballTeam() );
					teamPlayer.setFootballTeamName( shouFa.getFootballTeam().getFootballTeamName() );
					teamPlayer.setLiansai( ZuCaiUtils.getLianSaiById( shouFa.getLiansaiId() ) );
					teamPlayer.setLiansaiYear( ZuCaiUtils.getLianSaiYearById( shouFa.getLiansaiYearId() ) );
					teamPlayerList.add( teamPlayer );
				}
			}
			shouFa.setFootballPlayer( footballPlayer );
		}
		
		this.footballPlayerService.saveAll( footballPlayerList );
		this.teamPlayerService.saveAll( teamPlayerList );
	}

	@Override
	public ShouFaBean getShouFa(Long lianSaiYearId, Long lianSaiId,
			Long teamDzId, int lunci, String playerId) {
		
		DetachedCriteria criteria = DetachedCriteria.forClass( ShouFaBean.class, "shouFa" );
		criteria.createAlias( "shouFa.teamDz", "teamDz" );
		criteria.createAlias( "shouFa.footballPlayer", "footballPlayer" );
		
		criteria.add( Restrictions.eq( "teamDz.id", teamDzId ) );
		criteria.add( Restrictions.eq( "shouFa.liansaiYearId", lianSaiYearId ) );
		criteria.add( Restrictions.eq( "shouFa.liansaiId", lianSaiId ) );
		criteria.add( Restrictions.eq( "shouFa.lunci", lunci ) );
		criteria.add( Restrictions.eq( "footballPlayer.playerId", playerId ) );
		
		List<ShouFaBean> list = this.shouFaDao.findByCriteria(criteria);
		
		if( list.isEmpty() ){
			return null;
		}
		
		return list.get(0);
		
	}
	
	
	public void checkGameFireInfo( List<ShouFaBean> shuofaList , List<GameFireBean> gameFireList ){
		
		for( GameFireBean gameFire : gameFireList ){
						
			for( ShouFaBean shouFa : shuofaList ){ //球员号码相同，比赛相同

				//换人
				if( gameFire.getEvent().equals( "换入" ) || gameFire.getEvent().equals( "换出" ) ){
					if( shouFa.getJerseyNumber() == gameFire.getJerseyNumber() 
							&& shouFa.getTeamDz().getId() == gameFire.getTeamDz().getId() 
						&& gameFire.getFootballTeam().getId() == shouFa.getFootballTeam().getId()
						){
									
						gameFire.setFootballPlayer( shouFa.getFootballPlayer() );
						gameFire.setPersonName( shouFa.getPersonName() );
						
						if( gameFire.getEvent().equals( "换入" ) ){
							shouFa.setPlayingSumTime( 90-gameFire.getEventTime() );
						}else if( gameFire.getEvent().equals( "换出" ) ){
							shouFa.setPlayingSumTime( gameFire.getEventTime() );
						}
					}
				}else{
					if( shouFa.getPlayerId().equals( gameFire.getPlayerId() ) 
							&& shouFa.getTeamDz().getId() == gameFire.getTeamDz().getId() 
						&& gameFire.getFootballTeam().getId() == shouFa.getFootballTeam().getId()
						){
							
						gameFire.setFootballPlayer( shouFa.getFootballPlayer() );
						
					}
				}
				
			}
			
		}
		
	}

	@Override
	public List<ShouFaBean> searchShouFa( Long teamDzId, Long footballTeamId ) {

		DetachedCriteria criteria = DetachedCriteria.forClass( ShouFaBean.class, "shouFa" );
		criteria.createAlias( "shouFa.teamDz", "teamDz" );
		criteria.createAlias( "shouFa.footballPlayer", "footballPlayer" );
		
		criteria.add( Restrictions.eq( "teamDz.id", teamDzId ) );
		if( footballTeamId != null ){
			criteria.createAlias( "shouFa.footballTeam", "footballTeam" );
			criteria.add( Restrictions.eq( "footballTeam.id", footballTeamId ) );	
		}
		
		criteria.add( Restrictions.gt( "shouFa.playingSumTime", 0 ) );	
		
		criteria.addOrder( Order.desc( "footballTeam.id" ) );
		criteria.addOrder( Order.asc( "shouFa.shouFa" ) );
		criteria.addOrder( Order.desc( "footballPlayer.position" ) );
		return this.shouFaDao.findByCriteria(criteria);
	}
	
}
